<template>
  <div class="receiving">
    <!-- 导航栏 -->
    <van-nav-bar
      title="地址列表"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <!-- 地址列表 -->
    <Address>
      <div>
        <van-address-list
          :list="list"
          default-tag-text="默认"
          :switchable="false"
          @add="addAddress"
          @edit="editAddress"
        />

        <div v-if="list.length === 0">
          <van-empty description="请添加地址"></van-empty>
        </div>
      </div>
    </Address>
  </div>
</template>

<script>
import Address from "../components/Address.vue";
export default {
  name: "Receiving",

  components: {
    Address,
  },

  data() {
    return {
      //地址数据
      list: [],
    };
  },

  created() {
    this.getAddressList();
  },

  methods: {
    // 返回
    onClickLeft() {
      this.$router.push({ name: "My" });
    },

    //新增地址
    addAddress() {
      this.$router.push({ name: "Addreceiving" });
    },

    //编辑地址
    editAddress(item) {
      this.$router.push({ name: "Addreceiving", params: { aid: item.aid } });
    },

    //获取地址列表
    getAddressList() {
      this.axios({
        method: "get",
        url: "/findAddress",
      })
        .then((res) => {
          // 

          //   this.$toast(res.data.msg);

          //如果未登录,跳转到登录页面
          if (res.data.code === 700) {
            this.$router.push({ name: "Login" });
          }

          if (res.data.code === 20000) {
            res.data.result.forEach((v) => {
              v.address = `${v.province}${v.city}${v.county}${v.addressDetail}`;
              v.isDefault = Boolean(v.isDefault);
            });

            this.list = res.data.result;
          }
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
// 导航栏
/deep/.van-nav-bar {
  height: 40px;
}

/deep/.van-nav-bar__title {
  color: #37343b;
  font-weight: 600;
}

/deep/.van-nav-bar__text {
  color: #37343b;
  font-weight: 600;
}

/deep/.van-nav-bar .van-icon {
  color: #37343b;
}

// 地址列表
/deep/.van-address-list {
  padding: 0;
}

/deep/.van-address-item {
  padding: 16px 14px;
}

/deep/.van-address-item__edit {
  right: 0;
}

/deep/.van-address-item .van-radio__icon--checked .van-icon {
  background-color: #37343b;
  border-color: #37343b;
}

/deep/.van-button--danger {
  background-color: #37343b;
  border-color: #37343b;
  width: 300px;
  margin: auto;
}

/deep/.addressbox {
  width: 350px;
}
</style>